<?php
namespace Admin\Controller;
use Think\Controller;

class UserController extends Controller{
    public function login(){
        if(IS_POST){
            $account=I('post.account');
            $pwd=I('post.password');
            $code=I('post.yzm');
            $Verify=new \Think\Verify();
            $userModel=D('user');
            $user=$userModel->where(array('account'=>$account))->find();
            if(!$user){
                echo "<script>alert('此账号不存在!');location.href='javascript:history.go(-1)';</script>";
            }else{
                if($user['password']!==md5($pwd.$user['salt'])){
                    echo "<script>alert('密码错误!');location.href='javascript:history.go(-1)';</script>";
                }else{
                    if(!$Verify->check($code)){
                        echo "<script>alert('验证码错误!');location.href='javascript:history.go(-1)';</script>";
                    }else{
                        cookie('userid',$user['password']);
                        cookie('uid',$user['uid']);
                        cookie('account',$user['account']);
                        $userinfoModel=D('userinfo');
                        $userinfo=$userinfoModel->where(array('account'=>$account))->find();
                        cookie('name',$userinfo['name']);
                        $coo_kie=jm($user['uid'].$user['password'].C('COO_KIE'));
                        cookie('key',$coo_kie);
                        $con['uid']=$user['uid'];
                        $time['last_time']=date("Y-m-d H:i:s");
                        $userModel->where($con)->save($time);
                        $url=U('Index/index');
                        echo "<script>alert('登陆成功。');location.href='$url';</script>";
                    }
                }
            }
        }
        $this->display();
    }

    //退出登陆
    public function logout(){
        cookie('name',null);
        cookie('account',null);
        cookie('userid',null);
        cookie('key',null);
        cookie('uid',null);
        $url=U('User/login');
        echo "<script>alert('退出成功。');location.href='$url';</script>";
    }

    //验证码
    public function yzm(){
        $config =    array(
            'fontSize'    =>    25,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'useCurve'    =>    false,
        );
        $Verify=new \Think\Verify($config);
        $Verify->codeSet = '0123456789';
        $Verify->entry();
    }

    //注册账号
    public function reg(){
        if(IS_POST){
            $userModel=D('user');
            if(!$userModel->create()){
                $error=$userModel->getError();
                echo "<script>alert('$error');location.href='javascript:history.go(-1)';</script>";
                exit;
            }
            $s=$this->yan();
            $userModel->password=md5($userModel->password.$s);
            $userModel->salt=$s;
            $userModel->re_time=date("Y-m-d H:i:s",$userModel->re_time);
            $userModel->last_time=date("Y-m-d H:i:s",$userModel->last_time);
            $userinfoModel=D('userinfo');
            $userinfo['name']=I('post.name');
            $userinfo['account']=$con['account']=I('post.account');
            $userinfo['email']=I('post.email');
            if($userModel->add()){
                $user=$userModel->where($con)->find();
                $userinfo['uid']=$user['uid'];
                $userinfoModel->add($userinfo);
                $url=U('User/login');
                echo "<script>alert('注册成功。');location.href='$url';</script>";
            }
        }
        $this->display();
    }

    protected function yan(){
        $str='cno=-_l+aqweien,??<>.3;/.9479[]asd2><[37489ad';
        return substr(str_shuffle($str),0,8);

    }
}
?>